﻿//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Security.Cryptography;

#endregion

namespace GateShift.Tools.EncryptionKeyGen
{
    /// <summary>
    /// Base class for the EncryptionKeyGen application
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Processes the console application
        /// </summary>
        /// <param name="args">String array of arguments to process</param>
        /// <returns>Integer based on the success of the process</returns>
        public static int Main(string[] args)
        {
            // Validate we have an argument
            if (args.Length < 1)
            {
                PrintHelp();
                return -1;
            }

            // Validate our first argument is not empty
            if (string.IsNullOrEmpty(args[0]))
            {
                PrintHelp();
                return -1;
            }

            // Process Request
            Generate(args[0]);

            // Return success
            return 0;
        }

        /// <summary>
        /// Generates the new values and prints the output to the screen
        /// </summary>
        /// <param name="algorithmName">Name of the algorithm to create</param>
        private static void Generate(string algorithmName)
        {
            SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create(algorithmName);

            algorithm.GenerateIV();
            algorithm.GenerateKey();

            Console.WriteLine();
            Console.WriteLine("     Encryption Key: {0}", Convert.ToBase64String(algorithm.Key));
            Console.WriteLine("      Encryption IV: {0}", Convert.ToBase64String(algorithm.IV));
            Console.WriteLine();
            Console.WriteLine("           Key Size: {0}-Bit", algorithm.KeySize);
            Console.WriteLine("      Feedback Size: {0}-Bit", algorithm.FeedbackSize);
            Console.WriteLine("         Block Size: {0}-Bit", algorithm.BlockSize);
            Console.WriteLine("    Encryption Mode: {0}", algorithm.Mode);
            Console.WriteLine();
        }

        /// <summary>
        /// Prints the general usage of the application to the console window
        /// </summary>
        private static void PrintHelp()
        {
            Console.WriteLine();
            Console.WriteLine("Purpose:");
            Console.WriteLine("   Generates a usable IV and Key for use in encryption between");
            Console.WriteLine("   client and server and shows the codes in ASCII format.");
            Console.WriteLine();

            Console.WriteLine("Usage:");
            Console.WriteLine("   EncryptionKeyGen.exe [EncryptionConfigName]");
            Console.WriteLine();

            Console.WriteLine("   [EncryptionConfigName]");
            Console.WriteLine("      A list of default usable names can be found at from the ");
            Console.WriteLine("      system.security.cryptography.cryptoconfig on MSDN.");
            Console.WriteLine("         * Rijndael (AES) [Recommended]");
            Console.WriteLine("         * RC2");
            Console.WriteLine("         * TripleDES");
            Console.WriteLine("         * DES");
            Console.WriteLine();

            Console.WriteLine("NOTES:");
            Console.WriteLine("   * Custom configurations should be added to the App.Config first.");
            Console.WriteLine();
        }
    }
}
